//
//  PASecleteCardViewController.m
//  PAMobileWallet
//
//  Created by shen pu on 13-7-24.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PASecleteCardViewController.h"
#import "PASecleteCardCell.h"
#import "PADataCenter.h"
#import "PANetworkManager.h"
#import "PABankCardInfo.h"
#import "PANavView.h"
#import "PAAddCardViewController.h"
#import "PAStateType.h"

@interface PASecleteCardViewController ()

@end

@implementation PASecleteCardViewController
@synthesize secleteCardTable;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        navView = [[PANavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        navView.delegate = self;
        [navView setTitle:@"充值"];
        [self.view addSubview:navView];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        if (!dataCenter)
        {
            dataCenter = [[PADataCenter alloc] init];
        }
        
        CGFloat x, y, w, h;
        x = 10;  y = 44 + 10;  w = 300;  h = iPhone5?(548-44-10):(460-44-10) -20;
        secleteCardTable = [[UITableView alloc] initWithFrame:CGRectMake(x,y,w,h) style:UITableViewStylePlain];
        secleteCardTable.delegate = self;
        secleteCardTable.dataSource = self;
        secleteCardTable.backgroundView = nil;
        secleteCardTable.backgroundColor = [UIColor clearColor];
        secleteCardTable.showsHorizontalScrollIndicator = NO;
        secleteCardTable.showsVerticalScrollIndicator = YES;
        secleteCardTable.separatorStyle = UITableViewCellSeparatorStyleNone; //去掉横线
        [self.view addSubview:secleteCardTable];
        
        UIView * addview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
        addview.backgroundColor = [UIColor clearColor];
        
        UIButton * functionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [functionBtn setFrame:CGRectMake(5, 5, 580/2, 100/2)];
        [functionBtn setBackgroundImage:[UIImage imageNamed:@"card_addBT.png"]
                               forState:UIControlStateNormal];
        [functionBtn addTarget:self action:@selector(confirmClicked) forControlEvents:UIControlEventTouchUpInside] ;
        [addview addSubview:functionBtn];
        
        secleteCardTable.tableFooterView = addview;

    }
    return self;
}

#pragma mark - PANavViewDelegate methods

- (void)sendSelf:(PANavView *)sendView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendNavTitle:(NSString *)navTitleStr;
{
    if (navTitleStr)
    {
        [navView setTitle:navTitleStr];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark tab le view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [dataCenter.selfInfo.bankCardInfos count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return  120/2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    PASecleteCardCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell)
    {
        cell = [[PASecleteCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    /*
     if (didSelectRow == indexPath.row)
     {
     cell.secleteCardImage.hidden = YES;
     }
     */
    
    PABankCardInfo * bankCardInfo = [dataCenter.selfInfo.bankCardInfos objectAtIndex:indexPath.row];
    NSString * bankCardTypeStr;
    if (bankCardInfo.bankCardType == BCT_DEBIT_CARD)
    {
        bankCardTypeStr = @"信用卡";
    }
    
    if (bankCardInfo.bankCardType == BCT_CREDIT_CARD)
    {
        bankCardTypeStr = @"借记卡";
    }
    
    cell.cardNumLable.text = [NSString stringWithFormat:@"尾号为 %@",[bankCardInfo.bankCardNum substringFromIndex:[bankCardInfo.bankCardNum length] - 4]];
    cell.cardNameLable.text = [NSString stringWithFormat:@"%@",bankCardTypeStr];
    cell.cardlogoImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%@.png",bankCardInfo.bankMark]];
    
    return cell;
}

- (void)confirmClicked {
    PABaseViewController * controller = nil;
    if (!addCardViewController)
    {
        addCardViewController = [[PAAddCardViewController alloc] initWithNibName:nil bundle:nil];
        addCardViewController.parentController = self;
    }
    controller = addCardViewController;
    [[PAStateType sharedInstance] setValue:@"40" forKey:@"personalOrCharge"];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
    NSIndexPath * didselectpath = [NSIndexPath indexPathForRow:didSelectRow inSection:0];
    PASecleteCardCell * didselectcell = (PASecleteCardCell *)[secleteCardTable cellForRowAtIndexPath:didselectpath];
    didselectcell.secleteCardImage.hidden = YES;
    
    PASecleteCardCell * cell = (PASecleteCardCell *)[secleteCardTable cellForRowAtIndexPath:indexPath];
    cell.secleteCardImage.hidden = NO;
    didSelectRow = indexPath.row;
    */

    PABankCardInfo * bankCardInfo = [dataCenter.selfInfo.bankCardInfos objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOT_SelectedCard  object:bankCardInfo];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [self.secleteCardTable reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
